<template>
    <addEditApprovalCarView ref="addEditApprovalCarView" />
    <addFileView ref="addFileView" />
    <el-main>
        <div class="teacher">
            <div class="app-table">
                <div class="app-body">
                    <div class="app-search">
                        <el-form :inline="true" class="demo-form-inline" v-model="form">
                            <el-form-item label="人员姓名:">
                                <el-input class="serach_ipt" placeholder="请输入人员姓名"
                                    v-model="form.teacherName"></el-input>
                            </el-form-item>
                            <el-form-item label="租车部门:">
                                <el-input class="serach_ipt" placeholder="请输入租车部门"
                                    v-model="form.departmentName"></el-input>
                            </el-form-item>
                            <el-form-item label="审批状态:">
                                <el-select v-model="form.status" class="m-2 serach_ipt" placeholder="状态" style="width:190px;">
                                    <el-option v-for="item in options" :key="item.value" :label="item.label"
                                        :value="item.value" />
                                </el-select>
                            </el-form-item>

                            <el-form-item>
                                <el-button class="serachBtn" type="primary" @click="onSubmit"
                                    @keyup.enter="searchEnter">搜索</el-button>
                                <!-- <el-button type="success" @click="add">添加</el-button> -->
                                <el-button class="exportBtn" type="warning" @click="exportExcel">导出报表</el-button>
                                <!-- <el-button type="danger" @click="importExcel">批量导入<el-icon class="el-icon--right"><Upload /></el-icon></el-button> -->
                            </el-form-item>
                        </el-form>
                    </div>
                    <el-table ref="multipleTableRef" :data="list" style="width: 100%"
                        :row-class-name="tableRowClassName" @selection-change="handleSelectionChange"
                        :header-cell-style="{ 'font-size': '14px', 'color': '#000', }">
                        <el-table-column type="selection" width="30" />
                        <el-table-column type="index" width="60" label="序号" />
                        <el-table-column prop="teacherName" label="申请人" width="150">
                            <template #default="scope">
                                <div class="teacherNameStyle">{{ scope.row.teacherName }}</div>
                                <div class="departmentNameStyle">{{ scope.row.departmentName }}</div>
                            </template>
                        </el-table-column>
                        <el-table-column label="始发地-目的地">
                            <template #default="scope">
                                <div>{{ scope.row.leaveAddress }}-{{ scope.row.arrivalAddress }}</div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="description" label="申请事由" />
                        <el-table-column prop="createTime" label="申请时间" />
                        <el-table-column prop="startTime" label="用车时间">
                            <template #default="scope">
                                <div>{{ scope.row.startTime }}~{{ scope.row.endTime }}</div>
                            </template>
                        </el-table-column>
                        <el-table-column label="状态" width="90">
                            <template #default="scope">
                                <div v-if="scope.row.carDetail == null" class="status_column" style="color:#FF8400">
                                    <span>*</span>未派车</div>
                                <div v-if="scope.row.carDetail != null" class="status_column" style="color:#00c78c">
                                    <span>*</span>已派车</div>
                            </template>

                        </el-table-column>
                        <el-table-column label="操作" width="140px">
                            <template style="display: block" #default="scope">
                                <el-button class="editbtn" size="small" type="success"
                                    @click="editApprovalCar(scope.row)">编辑
                                </el-button>
                                <el-button class="delbtn" size="small" type="danger"
                                    @click="deleteApprovalCar(scope.row.id)">删除
                                </el-button>
                            </template>
                        </el-table-column>
                        <template v-slot:empty>
                            <el-empty description="暂无数据"></el-empty>
                        </template>
                    </el-table>


                    <el-config-provider :locale="locale">
                        <!-- <el-pagination :total="total" :page-size="limit" :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
                            layout="total, sizes, prev, pager, next, jumper" @size-change="changePageSize"
                            @current-change="changeCurrentPage">
                        </el-pagination> -->
                        <el-pagination class="page_box" v-model:current-page="page" v-model:page-size="pageSize"
                            :page-sizes="[5, 10, 20, 30, 40, 50, 100]" layout="total, sizes, prev, pager, next, jumper"
                            :total="total" @size-change="changePageSize" @current-change="changeCurrentPage" />
                    </el-config-provider>
                </div>
                <bottomView />
            </div>
        </div>
    </el-main>
</template>
<script>
import addEditApprovalCarView from "@/components/car/addEditApprovalCar.vue";
import addFileView from "@/components/addExcelFile.vue";
import { ElConfigProvider, ElMessageBox, ElMessage, ElTable } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { httpService } from "@/utils/httpService";
import { Picture as IconPicture, Upload } from "@element-plus/icons-vue";
import { ref } from 'vue'
import bottomView from "@/components/bottom.vue";

export default {
    name: "ApprovalCarListView",
    data() {
        return {
            type: 0,
            title: "",
            page: 0,
            pageSize: 10,
            total: 0,
            limit: 10,
            list: [],
            form: {
                description: "",
                arrivalAddress: "",
                teacherNumber: "",
                teacherName: "",
                departmentName: "",
                leaveAddress: "",
                urls: "",
                carDetail: "",
                carTeacherNumber: "",
                carTeacherName: "",
                status: ''
            },
            options: [
                {
                    label: '全部',
                    value: '',
                },
                {
                    label: '待审批',
                    value: '0',
                },
                {
                    label: '已审批',
                    value: '1',
                },
                {
                    label: '已驳回',
                    value: '2',
                },
            ],
            statusValue: "",
            multipleSelection: [],
            multipleTableRef: ref()
        };
    },
    components: {
        addEditApprovalCarView,
        addFileView,
        IconPicture,
        ElConfigProvider,
        ElMessageBox,
        ElMessage, Upload, bottomView
    },
    setup() {
        return {
            locale: zhCn,
        };
    },
    mounted() {
        this.getApprovalCarList();
    },
    watch: {
        $route() {
            this.getApprovalCarList();
        },
    },
    methods: {
        searchEnter: function (e) {
            // 使用 which 和 keyCode 属性来解决兼容问题
            var keyCode = window.event ? e.keyCode : e.which;
            var val = e.target.value;
            if (keyCode == 13 && val) {
                this.getApprovalCarList();
            }
        },
        handleSelectionChange(val) {
            this.multipleSelection = val
            console.log(this.multipleSelection);
        },
        add() {
            this.$refs.addEditApprovalCarView.title = "添加租车申请表"
            this.$refs.addEditApprovalCarView.personnelType = this.type
            this.$refs.addEditApprovalCarView.dialogFormVisible = true;
        },
        editApprovalCar(obj) {
            console.log(obj);
            this.$refs.addEditApprovalCarView.title = "修改信息"
            this.$refs.addEditApprovalCarView.getApprovalCarById(obj.id);
            this.$refs.addEditApprovalCarView.dialogFormVisible = true;
        },
        deleteApprovalCar(id) {
            ElMessageBox.confirm("确定要删除本条记录吗？", "提醒", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    httpService("/javaApi/approvalCar/deleteById", "id=" + id, "post").then(
                        (data) => {
                            if (data.code == 200) {
                                ElMessage.success("删除成功");
                                this.getApprovalCarList();
                            } else {
                                ElMessage.error("删除失败，请重试！");
                            }
                        }
                    );
                })
                .catch(() => {
                });
        },
        reloadData() {
            this.page = 1;
            this.getApprovalCarList()
        },
        importExcel() {
            this.$refs.addFileView.dialogFormVisible = true;
            this.$refs.addFileView.title = "批量导入租车申请表";
            this.$refs.addFileView.uploadApiUrl = "/javaApi/approvalCar/importExcel";
        },
        exportExcel() {
            httpService(
                "/javaApi/approvalCar/exportExcel",
                {
                    // "description": this.form.description,
                    // "arrivalAddress": this.form.arrivalAddress,
                    // "teacherNumber": this.form.teacherNumber,
                    "teacherName": this.form.teacherName,
                    "departmentName": this.form.departmentName,
                    // "leaveAddress": this.form.leaveAddress,
                    // "urls": this.form.urls,
                    // "carDetail": this.form.carDetail,
                    // "carTeacherNumber": this.form.carTeacherNumber,
                    // "carTeacherName": this.form.carTeacherName,
                },
                "get"
            ).then((data) => {
                console.log(data);
                if (data.code == 200) {
                    window.open(data.message, '_blank')
                }
            });
        },
        onSubmit() {
            this.getApprovalCarList();
        },
        // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
        changePageSize(size) {
            this.pageSize = size;
            this.limit = size;
            this.getApprovalCarList();
        },

        // 改变页码，page：回调参数，表示当前选中的“页码”
        changeCurrentPage(page) {
            this.page = page;
            this.getApprovalCarList();
        },
        tableRowClassName({ row, rowIndex }) {
            return "";
        },
        getApprovalCarList() {
            httpService(
                "/javaApi/approvalCar/list/" + this.page + "/" + this.pageSize,
                {
                    "description": this.form.description,
                    "arrivalAddress": this.form.arrivalAddress,
                    "teacherNumber": this.form.teacherNumber,
                    "teacherName": this.form.teacherName,
                    "departmentName": this.form.departmentName,
                    "leaveAddress": this.form.leaveAddress,
                    "urls": this.form.urls,
                    "carDetail": this.form.carDetail,
                    "carTeacherNumber": this.form.carTeacherNumber,
                    "carTeacherName": this.form.carTeacherName,
                    "status": this.form.status,
                },
                "get"
            ).then((data) => {
                console.log(data);
                this.list = data.object.list;
                this.total = data.object.total;
                this.pageCount = data.object.pages;
            });
        },
    },
};
</script>
<style scoped>
.el-main {
    padding: 0px;
}

.custom-header {
    color: red;
    font-weight: bold;
}

.el-table thead tr th {
    background-color: #E5E5EF;
    margin: 0;
    font-size: 14px;
    color: #cc3636;
    font-weight: bold;
}

.teacher-content {
    width: 100%;
}
.app-body {
  background: white;
  padding: 20px 20px 10px 20px;
  margin-bottom: 10px;
  min-height: calc(100vh - 244px);
}
.app-table {
    height: 90vh;
    background: #eff3f5;
    padding: 10px;
    overflow: hidden;
    overflow: auto;
}

.app-table .page_box {
    background-color: #fff;
    margin-top: -10px;
    padding: 40px 0 10px 20px;
}

.app-search {
    padding-top: 20px;
    margin-top: 20px;
    background-color: #fff;
    border-radius: 8px 8px 0 0;
    padding-left: 20px;
}

.teacherNameStyle {
    color: #2A293F;
    font-size: 16px;
}

.departmentNameStyle {
    color: #007EFF;
    font-size: 12px;
}

.status_column {
    display: flex;
}

.status_column span {
    display: block;
    padding-top: 3px;
}

.serachBtn {
    background-color: #007EFF;
    border-radius: 8px;
    border: none;
}

.exportBtn {
    background-color: #FF7200;
    border-radius: 8px;
    border: none;
}

.editbtn {
    background-color: #fff;
    color: #007EFF;
    border: none;
}

.app-search .serach_ipt {
    border-radius: 8px;
    background-color: #FFFFFF;
    color: #B2BFCC;
}

.delbtn {
    background-color: #fff;
    color: #FF0000;
    border: none;
}

.app-separator {
    padding-top: 10px;
    padding-bottom: 20px;
    padding-left: 10px;
}

.app-body {
    background: white;
    padding: 20px 20px 10px 20px;
    margin-bottom: 10px;
}

.el-table .warning-row {
    --el-table-tr-bg-color: var(--el-color-warning-lighter);
}

.el-table .success-row {
    --el-table-tr-bg-color: var(--el-color-success-lighter);
}

.el-pagination {
    text-align: center;
    margin-top: 10px;
}
</style>
